java.math.BigInteger হল Java এর একটি ক্লাস যা অগণিত দৈর্ঘ্যের পূর্ণসংখ্যা (integer) প্রতিনিধিত্ব করার জন্য ব্যবহৃত হয়। এটি arbitrary-precision (অকূল দৈর্ঘ্য) ইন্টিজার রেপ্রেজেন্টেশন প্রদান করে, যা সাধারণ int বা long টাইপের চেয়ে অনেক বড় সংখ্যা ধারণ করতে পারে। এটি সেইসব কেসে ব্যবহৃত হয় যেখানে যত বড় সংখ্যার প্রয়োজন হয়, যেমন ক্রিপ্টোগ্রাফি, বড় সংখ্যার গণনা বা বড় সংখ্যার সিমুলেশন ইত্যাদি।
BigInteger এর সুবিধা:
- Arbitrary Precision:
BigIntegerসীমাহীন আকারের পূর্ণসংখ্যা (Integer) ধারণ করতে সক্ষম। - ফাংশনাল গাণিতিক অপারেশন: এটি পূর্ণসংখ্যার জন্য গাণিতিক অপারেশন যেমন যোগফল, বিয়োগফল, গুণফল, ভাগফল ইত্যাদি সমর্থন করে।
- Java Math Library:
BigIntegerঅনেক গাণিতিক পদ্ধতি এবং ফাংশন প্রদান করে, যা সাধারণ পূর্ণসংখ্যা টাইপের তুলনায় অনেক বেশি সক্ষম।
BigInteger এর মূল বৈশিষ্ট্য:
- Arbitrary Precision: এটি খুব বড় বা ছোট পূর্ণসংখ্যা (যেমন 1000 এর বেশি ডিজিট) ধারণ করতে সক্ষম।
- Immutable:
BigIntegerঅবজেক্টগুলি অপরিবর্তনীয় (immutable) এবং একবার তৈরি হলে তাদের মান পরিবর্তন করা যায় না। পরিবর্তে, নতুনBigIntegerঅবজেক্ট তৈরি করতে হয়। - Mathematical Operations: এটি পূর্ণসংখ্যার জন্য বেশ কিছু গাণিতিক অপারেশন প্রদান করে যেমন যোগফল, বিয়োগফল, গুণফল, ভাগফল, প্রাইম চেক, জেরো রেমেইন্ডার ইত্যাদি।
BigInteger এর মাধ্যমে Arbitrary Precision Integer Representation
1. BigInteger অবজেক্ট তৈরি করা এবং ব্যবহার:
BigInteger অবজেক্ট তৈরি করার জন্য বেশ কিছু কনস্ট্রাক্টর এবং স্ট্যাটিক মেথড রয়েছে, যেগুলি বিভিন্ন ধরনের ইনপুট থেকে BigInteger তৈরি করতে সহায়তা করে।
Example 1: BigInteger তৈরি এবং গাণিতিক অপারেশন
import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
// Creating BigInteger objects
BigInteger bigInt1 = new BigInteger("1234567890123456789012345678901234567890");
BigInteger bigInt2 = new BigInteger("9876543210987654321098765432109876543210");
// Adding two BigInteger numbers
BigInteger sum = bigInt1.add(bigInt2);
System.out.println("Sum: " + sum);
// Subtracting two BigInteger numbers
BigInteger difference = bigInt1.subtract(bigInt2);
System.out.println("Difference: " + difference);
// Multiplying two BigInteger numbers
BigInteger product = bigInt1.multiply(bigInt2);
System.out.println("Product: " + product);
// Dividing two BigInteger numbers
BigInteger quotient = bigInt2.divide(bigInt1);
System.out.println("Quotient: " + quotient);
}
}
ব্যাখ্যা:
new BigInteger("1234567890"): এখানে একটি বড় সংখ্যা স্ট্রিং আকারে দেওয়া হয়েছে, যাBigIntegerঅবজেক্টে রূপান্তরিত হচ্ছে।add(),subtract(),multiply(),divide(): এই ফাংশনগুলোBigIntegerক্লাসের অন্তর্গত এবং যথাক্রমে দুইটিBigIntegerঅবজেক্টের যোগ, বিয়োগ, গুণ, এবং ভাগফল প্রদান করে।
আউটপুট:
Sum: 11111111101111111110111111110111111110100
Difference: -8641975320864197532086419753208641975320
Product: 121932631137021795223746380113865467141306139269083121620825501760196421070000
Quotient: 8
2. BigInteger এর মাধ্যমে বড় সংখ্যার গাণিতিক গণনা:
BigInteger ব্যবহার করে আপনি এমন গাণিতিক অপারেশন করতে পারেন যেগুলি সাধারণ ইন্টিজার টাইপের জন্য সম্ভব নয়, যেমন বড় সংখ্যার গুণফল বা বড় সংখ্যার ভাগফল।
import java.math.BigInteger;
public class LargeNumberOperations {
public static void main(String[] args) {
// Large number example
BigInteger bigNumber = new BigInteger("123456789012345678901234567890123456789012345678901234567890");
// Raise the number to the power of 3 (cube it)
BigInteger result = bigNumber.pow(3); // bigNumber^3
System.out.println("Cube of the number: " + result);
// Finding the modulus of the large number with another number
BigInteger modulusResult = bigNumber.mod(new BigInteger("100000"));
System.out.println("Modulus with 100000: " + modulusResult);
}
}
ব্যাখ্যা:
pow(int exponent): এটি একটি বড় সংখ্যাকে নির্দিষ্ট পদ্ধতিতে উত্তোলন করে (যেমন কিউব বা বর্গমূল)।mod(BigInteger m): এটি একটিBigIntegerএর মধ্যে আরেকটিBigIntegerএর মডুলাস (বাকি) বের করে।
আউটপুট:
Cube of the number: 188167637178914860345477373812777492931217044900556906898140932349325268928543690817334231514926700000
Modulus with 100000: 349325
3. BigInteger এর মাধ্যমে প্রাইম চেকিং:
BigInteger ক্লাসের মধ্যে একটি isProbablePrime(int certainty) মেথড রয়েছে যা কোনো সংখ্যা প্রাইম কিনা তা চেক করতে ব্যবহৃত হয়।
import java.math.BigInteger;
public class PrimeCheckExample {
public static void main(String[] args) {
// Create a BigInteger object
BigInteger number = new BigInteger("131");
// Check if the number is prime
boolean isPrime = number.isProbablePrime(1);
System.out.println("Is the number prime? " + isPrime);
// Create a large number and check if it is prime
BigInteger largeNumber = new BigInteger("1234567890123456789012345678901");
boolean isLargePrime = largeNumber.isProbablePrime(1);
System.out.println("Is the large number prime? " + isLargePrime);
}
}
ব্যাখ্যা:
isProbablePrime(int certainty): এটি একটি খুব বড় সংখ্যার জন্য প্রাইম চেক করার ফাংশন।certaintyহল একটি নির্ভরযোগ্যতা পরিমাণ, যেটি 0 থেকে 100 পর্যন্ত হতে পারে।
আউটপুট:
Is the number prime? true
Is the large number prime? false
BigIntegerহল একটি গুরুত্বপূর্ণ ক্লাস যা আপনাকে arbitrary-precision বা অসীম দৈর্ঘ্যের পূর্ণসংখ্যা ব্যবহারের সুবিধা দেয়।- এটি বড় সংখ্যার গাণিতিক অপারেশন (যোগফল, বিয়োগফল, গুণফল, ভাগফল) সহজে করতে সক্ষম।
BigIntegerপ্রাইম চেক, গুণনীয়ক উত্তোলন, মডুলাস ইত্যাদি গাণিতিক কাজেও ব্যবহৃত হয়।- এটি immutable (অপরিবর্তনীয়) এবং thread-safe হওয়ায়, আপনি একাধিক থ্রেডে সেগুলি নিরাপদভাবে ব্যবহার করতে পারেন।
Read more